// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PostRepositoryJdbc.java

package dale.web.post.repository;

import dale.web.category.domain.Category;
import dale.web.domain.Condition;
import dale.web.post.domain.Post;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.*;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

// Referenced classes of package dale.web.post.repository:
//            PostRepository, PostRowMapper

public class PostRepositoryJdbc extends NamedParameterJdbcDaoSupport
    implements PostRepository
{

    public PostRepositoryJdbc(DataSource dataSource)
    {
        setDataSource(dataSource);
    }

    public int max()
    {
        String MAX = "SELECT MAX(postNo) FROM post";
        return getJdbcTemplate().queryForInt(MAX);
    }

    public int after(int no)
    {
        String AFTER = "SELECT max(postNo) FROM post WHERE postNo < ?";
        return getJdbcTemplate().queryForInt(AFTER, new Object[] {
            Integer.valueOf(no)
        });
    }

    public int before(int no)
    {
        String BEFORE = "SELECT min(postNo) FROM post WHERE postNo > ?";
        return getJdbcTemplate().queryForInt(BEFORE, new Object[] {
            Integer.valueOf(no)
        });
    }

    public int insert(Post post)
    {
        String INSERT = "INSERT INTO post (postNo, categoryID, title, content, userID, date, hit)\nVALUES (NULL, :category.categoryID, :title, :content, 'dale.seo', now(), 0)";
        org.springframework.jdbc.core.namedparam.SqlParameterSource param = new BeanPropertySqlParameterSource(post);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(INSERT, param, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(Post post)
    {
        String UPDATE = "UPDATE post\nSET categoryID = :category.categoryID, title = :title, content = :content, date = now()WHERE postNo = :postNo";
        org.springframework.jdbc.core.namedparam.SqlParameterSource param = new BeanPropertySqlParameterSource(post);
        return getNamedParameterJdbcTemplate().update(UPDATE, param);
    }

    public int delete(int no)
    {
        String DELETE = "DELETE FROM post WHERE postNo = ?";
        return getJdbcTemplate().update(DELETE, new Object[] {
            Integer.valueOf(no)
        });
    }

    public Post item(int no)
    {
        String sql = "SELECT * FROM post\nLEFT JOIN category USING (categoryID) WHERE postNo =\t?";
        try
        {
            return (Post)getJdbcTemplate().queryForObject(sql, new PostRowMapper(), new Object[] {
                Integer.valueOf(no)
            });
        }
        catch(EmptyResultDataAccessException e)
        {
            return new Post();
        }
    }

    public List list(Post post, Condition condition)
    {
        String sql = "SELECT * FROM post\nLEFT JOIN category USING (categoryID)";
        MapSqlParameterSource param = new MapSqlParameterSource();
        sql = (new StringBuilder(String.valueOf(sql))).append("\n WHERE (title LIKE :wildKey OR title LIKE :wildKey)").toString();
        param.addValue("wildKey", condition.getWildKey());
        if(post.getCategory() != null)
        {
            String categoryID = post.getCategory().getCategoryID();
            if(StringUtils.isNotBlank(categoryID))
                sql = (new StringBuilder(String.valueOf(sql))).append("\n AND categoryID = :categoryID").toString();
            param.addValue("categoryID", categoryID);
        }
        sql = (new StringBuilder(String.valueOf(sql))).append("\n ORDER BY postNo DESC LIMIT :offset, :rowcnt").toString();
        param.addValue("offset", Integer.valueOf(condition.getOffset()));
        param.addValue("rowcnt", Integer.valueOf(condition.getRowcnt()));
        return getNamedParameterJdbcTemplate().query(sql, param, new PostRowMapper());
    }

    public int count(Post post, Condition condition)
    {
        String COUNT = "SELECT COUNT(*) FROM post\nLEFT JOIN category USING (categoryID)";
        MapSqlParameterSource param = new MapSqlParameterSource();
        String sql = (new StringBuilder(String.valueOf(COUNT))).append("\n WHERE (title LIKE :wildKey OR title LIKE :wildKey)").toString();
        param.addValue("wildKey", condition.getWildKey());
        if(post.getCategory() != null)
        {
            String categoryID = post.getCategory().getCategoryID();
            if(StringUtils.isNotBlank(categoryID))
                sql = (new StringBuilder(String.valueOf(sql))).append("\n AND categoryID = :categoryID").toString();
            param.addValue("categoryID", categoryID);
        }
        int count = getNamedParameterJdbcTemplate().queryForInt(sql, param);
        return count;
    }

    private static final String SELECT = "SELECT * FROM post\nLEFT JOIN category USING (categoryID)";
}
